package org.szgb.console.ht.electricity.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.ht.electricity.entity.HtElectricityUsage;
import org.szgb.console.ht.electricity.mapper.HtElectricityUsageMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class HtElectricityUsageService {
    private final HtElectricityUsageMapper htElectricityUsageMapper;

    @Autowired
    public HtElectricityUsageService(HtElectricityUsageMapper htElectricityUsageMapper){
        this.htElectricityUsageMapper = htElectricityUsageMapper;
    }

    @Transactional(readOnly = true)
    public HtElectricityUsage get(String id) {
        return htElectricityUsageMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtElectricityUsage> findList(HtElectricityUsage htElectricityUsage) {
        if (htElectricityUsage != null && htElectricityUsage.getPageDto() != null) {
            Page<HtElectricityUsage> page = new Page<>(htElectricityUsage.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htElectricityUsage.setPage(page);
        }
        return htElectricityUsageMapper.findList(htElectricityUsage);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtElectricityUsage htElectricityUsage) {
        return htElectricityUsageMapper.getCount(htElectricityUsage);
    }

    @Transactional()
    public void save(HtElectricityUsage htElectricityUsage) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htElectricityUsage.getId() == null || htElectricityUsage.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htElectricityUsage.setCreatedBy(htElectricityUsage.getUpdatedBy());
            // 添加UUID
            htElectricityUsage.setId(UUIDUtil.getUUID());
            // 执行插入操作
            htElectricityUsageMapper.insert(htElectricityUsage);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htElectricityUsageMapper.update(htElectricityUsage);
        }
    }

    @Transactional
    public void delete(String ids) {
        htElectricityUsageMapper.delete(ids.split(","));
    }
}
